#!/bin/bash
#
# chkconfig: - 85 15
# description: tracd
# processname: tracd
# Default-Start: 3 5 
# Default-Stop: 0 1 2 6 
# pidfile: /h01/trac/tracd.pid

set -e

PIDFILE=/h01/trac/tracd.pid
LOCKFILE=${LOCKFILE-/h01/trac/tracd.lock}
RETVAL=0

TRAC_PORT=3000
TRAC_USER=h01
TRAC_PROJECTS=/h01/trac/projects
SU="su $TRAC_USER --command "

#-------------------------------------------------------------------------------
# Start
#-------------------------------------------------------------------------------
start() {
    echo -n $"Starting tracd: "
    $SU "tracd --daemonize --pidfile=$PIDFILE --port=$TRAC_PORT -e $TRAC_PROJECTS > /dev/null 2>&1 &"
    RETVAL=$?
    if [ $RETVAL = 0 ];
    then
        echo "true"
        touch ${LOCKFILE}
    else
        echo "false"
    fi
    return $RETVAL
}
#-------------------------------------------------------------------------------
# Status
#-------------------------------------------------------------------------------
status() {
    if [ -a $PIDFILE ]
    then
        PID=(`cat $PIDFILE`)
        echo "tracd is running as pid $PID"
    else
        echo "tracd is not running"
    fi
}
#-------------------------------------------------------------------------------
# Stop
#-------------------------------------------------------------------------------
stop() {
    if [ -a $PIDFILE ]
    then
        echo -n $"Stopping tracd: "
        kill -9 `cat ${PIDFILE}`
        RETVAL=$?
        if [ $RETVAL = 0 ];
        then
            echo "true"
        else
            echo "false"
        fi
        rm -f ${LOCKFILE} ${PIDFILE}
    else
        echo "tracd is not running."
    fi
    return $RETVAL
}
#-------------------------------------------------------------------------------
# Application entry
#-------------------------------------------------------------------------------
case "$1" in
    start)
        # silently restarts
        stop > /dev/null 2>&1
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start
        ;;
    status)
        status
        ;;
    *)
        echo $"Usage: $NAME {start|stop|status|restart|help}"
        exit 1
esac

exit $RETVAL
